package com.hxy.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.common.JsonResult;
import com.hxy.pojo.Paydetail;
import com.hxy.pojo.vo.StuffinfoAddVo;
import com.hxy.pojo.vo.StuffinfoShowByIdVo;
import com.hxy.pojo.vo.StuffinfoVo;
import com.hxy.service.StuffinfoService;
import com.hxy.service.UserInfService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 员工信息控制
 *
 * @author feiyu
 */
@Controller
@RequestMapping("user")
public class UserListController {
    @Autowired
    private StuffinfoService stuffinfoService;
    @Autowired
    private UserInfService userInfService;

    /**
     * 员工信息列表
     *
     * @param page
     * @param limit
     * @param sex
     * @param stuffName
     * @param worklength
     * @param branch
     * @return
     */
    @RequestMapping("/show")
    @ResponseBody
    public JsonResult userList(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                               @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
                               @RequestParam(value = "sex", required = false, defaultValue = "") String sex,
                               @RequestParam(value = "stuffName", required = false, defaultValue = "") String stuffName,
                               @RequestParam(value = "worklength", required = false, defaultValue = "") String worklength,
                               @RequestParam(value = "branch", required = false, defaultValue = "") String branch) {
        System.out.println(page + " " + limit + " " + sex + stuffName + worklength + branch);
        page = page == null ? 1 : page;
        PageInfo<StuffinfoVo> stuffinfoPageInfo = stuffinfoService.selectByPageInfo(page, limit, sex, stuffName, worklength, branch);
        return new JsonResult(0, "", stuffinfoPageInfo);
    }

    /**
     * 员工添加
     *
     * @param StuffInfo
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public JsonResult show(StuffinfoAddVo StuffInfo) {
        int insert = stuffinfoService.insert(StuffInfo);
        System.out.println(StuffInfo);
        return new JsonResult(0, "success", null);
    }

    /**
     * 员工删除
     *将stuffinfo的status改为 2(解聘)
     * @param Eid
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Integer Eid) {
        System.out.println("删除?/////:" + Eid);
        int i = stuffinfoService.deleteByPrimaryKey(Eid);
        return new JsonResult(0, "success", i);
    }

    /**
     * 员工详情byid
     * 前台传id
     * 返回详情(显示的是name不是id)
     *
     * @param id
     * @return
     */
    @RequestMapping("showbyid")
    @ResponseBody
    public JsonResult update(Integer id) {
        System.out.println(id);
        //通过id获取StuffinfoAddVo原型带id的
        StuffinfoAddVo stuffinupdatefo = stuffinfoService.selectByIdeVo(id);
        //通过StuffinfoAddVo原型带id的去查找各自名字返回到前端
        // StuffinfoShowByIdVo stuffinfoShowByIdVos= stuffinfoService.selectByIdShowAllNames(stuffinupdatefo);
        return new JsonResult(0, "success", stuffinupdatefo);
    }

    /** 修改者 陈雄
     *  如果返回空，有可能是异常，打印出异常信息，排查
     * 返回当前登录的个人信息
     * @return
     */
    @RequestMapping("returnOneInfo")
    @ResponseBody
    public JsonResult returnOneInfo() {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        try {
            Integer eidByUsername = userInfService.findEidByUsername(username);//5
            StuffinfoShowByIdVo stuffinfoShowByIdVo = stuffinfoService.selectByIdShowAllNamesByEID(eidByUsername);
            if (stuffinfoShowByIdVo != null) {
                return new JsonResult(0, "success", stuffinfoShowByIdVo);
            } else {
                return new JsonResult(1, "fail", null);
            }
        }
        catch (Exception e){
            e.printStackTrace();
            return new JsonResult(1, "fail", null);
        }

    }

    /**
     * 回显用户详情
     * @return
     */
    @RequestMapping("returnOneDetail")
    @ResponseBody
    public JsonResult returnOneDetail() {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        Integer eidByUsername = userInfService.findEidByUsername(username);

        return userInfService.findOnePaydetailsByEid(eidByUsername);
    }
}
